package net.jxtz.jxtzos.spider.process;


import io.swagger.models.auth.In;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.jxtz.jxtzos.entity.spider.SpiderLogServe;
import net.jxtz.jxtzos.spider.BaseSpiderProcessor;
import net.jxtz.jxtzos.spider.SpiderDefinition;
import net.jxtz.jxtzos.spider.SpiderFactory;
import net.jxtz.jxtzos.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.BasicHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
@Slf4j
public class LandTransactionImageSpiderProcessor implements BaseSpiderProcessor {

    private final static CloseableHttpClient CLIENT = HttpClients.createDefault();

    public final static String SPIDER_NAME = "landTransactionImageSpiderProcessor";

    public LandTransactionImageSpiderProcessor() {
        SpiderFactory instance = SpiderFactory.getInstance();
        instance.putSpiderProcess(SPIDER_NAME, this);
        SpiderDefinition nanchangHousingAuthority = new SpiderDefinition("土地交易出让文件和图片", SPIDER_NAME);
        instance.putSpiderDefinition(SPIDER_NAME, nanchangHousingAuthority);
    }

    @Override
    public void start(SpiderLogServe spiderLogServe, SpiderDefinition spiderDefinition) throws IOException, Exception {
        for (int i = 1; i <= 1000; i++) {
            boolean process = process(i);
            if (!process) {
                break;
            }
        }
    }

    private boolean process(Integer pageNum) {
        String url = "http://jxgtjy.jxsggzy.cn/index.jsp?pageNo=" + pageNum;
        String s = httpClientIndex(url);
        Document parse = Jsoup.parse(s);
        Elements fPageNowRow = parse.getElementsByClass("fPageNowRow");
        String fileBaseUrl = "/Users/a123/Downloads/土地交易文件/";

        for (Element element : fPageNowRow) {

            Elements dkbh = element.getElementsContainingOwnText("地块编号");

            Elements dkmc = element.getElementsContainingOwnText("地块名称");
            if (!(dkbh.size() > 0 && dkmc.size() > 0)) {
                continue;
            }
            String s1 = dkbh.get(0).text() + "---" +dkmc.get(0).text();
            String fileName = null;
            try {
                fileName = URLDecoder.decode(fileBaseUrl + s1 + "/","UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            log.info(fileName);

            // 获取id
            Element button = element.getElementById("button");
            String onclick = button.attr("onclick");
            String regEx = "[^0-9]";
            Pattern compile = Pattern.compile(regEx);
            Matcher matcher = compile.matcher(onclick);

            // id
            String trim = matcher.replaceAll("").trim();

            String detailUrl = "http://jxgtjy.jxsggzy.cn/html/Documents.jsp?id=" + trim + "&lx=TBL_LANDINFO#";

            HttpGet httpGet = new HttpGet(detailUrl);
            String s2 = null;
            try {
                TimeUnit.SECONDS.sleep(1);
                CloseableHttpResponse response = CLIENT.execute(httpGet);
                s2 = ResponseUtil.entityToString(response.getEntity());
            } catch (IOException | InterruptedException e) {
                e.printStackTrace();
            }

            if (StringUtils.isNoneEmpty(s2)) {
                // 解析文件列表
                Document parse1 = Jsoup.parse(s2);
                Element mainTable = parse1.getElementsByClass("MainTable").get(0);

                Elements mainTR1 = mainTable.getElementsByClass("MainTR1");
                for (int i = 1; i < mainTR1.size(); i++) {
                    Element element1 = mainTR1.get(i);
                    Elements td = element1.getElementsByTag("td");

                    // 文件名称
                    String s4 = td.get(2).getElementsByTag("a").text();

                    String pattern = "[\\s]";
                    Pattern p = Pattern.compile(pattern);
                    Matcher m = p.matcher(s4);
                    s4 = m.replaceAll("");

                    s4 = s4.replaceAll("&nbsp;", "");

                    // 地址
                    Element a = td.get(2).getElementsByTag("a").get(0);
                    String onClick = a.attr("onClick");
                    int i1 = onClick.indexOf("'");
                    int i2 = onClick.lastIndexOf("'");
                    String url2 = "http://jxgtjy.jxsggzy.cn/" + onClick.substring(i1 + 1, i2);

                    log.info(url2);

                    try {
                        TimeUnit.SECONDS.sleep(1);
                        URL url1 = new URL(url2);

                        HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
                        urlConnection.setConnectTimeout(3 * 1000);
                        urlConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

                        InputStream inputStream = urlConnection.getInputStream();
                        byte[] bytes = readInputStream(inputStream);

                        File file = new File(fileName);
                        if (!file.exists()) {
                            file.mkdirs();
                        }

                        File file1 = new File(file, s4);

                        FileOutputStream fileOutputStream = new FileOutputStream(file1);
                        fileOutputStream.write(bytes);

                        fileOutputStream.close();
                        inputStream.close();
                    } catch (IOException | InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }

        }

        return true;
    }

    @SneakyThrows
    private String httpClientIndex(String url) {
        TimeUnit.SECONDS.sleep(3);
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> list = new ArrayList<>();
        list.add(new BasicNameValuePair("res_type_value", ""));
        list.add(new BasicNameValuePair("scope", ""));
        list.add(new BasicNameValuePair("rdbProgressCon", "V"));
        list.add(new BasicNameValuePair("xzqdm", ""));
        list.add(new BasicNameValuePair("xzqdmMcCon", ""));
        list.add(new BasicNameValuePair("onlyUser", ""));
        list.add(new BasicNameValuePair("querys", "nouse"));
        list.add(new BasicNameValuePair("bb_no", ""));
        list.add(new BasicNameValuePair("f_no", ""));
        list.add(new BasicNameValuePair("f_beigintime1", ""));
        list.add(new BasicNameValuePair("f_beigintime2", ""));
        list.add(new BasicNameValuePair("f_closetime1", ""));
        list.add(new BasicNameValuePair("f_closetime2", ""));
        list.add(new BasicNameValuePair("f_bailtime1", ""));
        list.add(new BasicNameValuePair("f_bailtime2", ""));
        list.add(new BasicNameValuePair("f_pos", ""));
        list.add(new BasicNameValuePair("fxzqmc", "请输入行政区名称或拼音首字母"));
        list.add(new BasicNameValuePair("rdbProgress", "V"));
        list.add(new BasicNameValuePair("f_notxt", ""));
        list.add(new BasicNameValuePair("SearchPageNo", "1"));
        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list);
        httpPost.setEntity(urlEncodedFormEntity);
        CloseableHttpResponse response = CLIENT.execute(httpPost);
        HttpEntity entity = response.getEntity();
        return ResponseUtil.entityToString(entity);
    }

    private byte[] readInputStream(InputStream inputStream) throws IOException {
        byte[] buffer = new byte[1024];
        int len = 0;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while((len = inputStream.read(buffer)) != -1) {
            bos.write(buffer, 0, len);
        }
        bos.close();
        return bos.toByteArray();
    }
}
